home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Boxer / PalmBoxer / makedb.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  5.7 KB  |  252 lines

  1. #define IGNORE_STDIO_STUBS
  2. #define __string_h
  3.  
  4. #ifdef OLDGCC
  5.  
  6. #include <Common.h>
  7. #include <System/SysAll.h>
  8. #include <UI/UIAll.h>
  9. #include <Unix/sys_types.h>
  10.  
  11. #else
  12.  
  13. #include <PalmOS.h>
  14. #include <PalmCompatibility.h>
  15. #include <Unix/sys_types.h>
  16.  
  17. #endif
  18.  
  19. #include "stringil.h"
  20. #include "stdio2.h"
  21.  
  22. struct prchead {
  23.   char name[32];                //0-32
  24.   short int attr;               //32-33
  25.   short int vers;               //34-35
  26.   long int cr, md, bkt;         //times 36-47
  27.   long int mn, app, sort;       // zero 48-59 - zero for prcs.
  28.   long int type, crea;          //60-67
  29.   long int uidseed, nxrec;      //68-75 - uidseed rand, nxrec zero;
  30.   short int nrecs;              //76-78
  31. } head;
  32.  
  33. struct rsrcdbent {
  34.   long int rsrc;
  35.   short int rsid;
  36.   long int ofst;                //80+10*(nrecs+index)
  37. } rsrcent, rsrcnxt;
  38.  
  39. struct normdbent {
  40.   long int ofst, uid;           //80+8*(nrecs+index)
  41. } dataent, datanxt;
  42.  
  43. int MakeDatabase(FILE * fd, int destruct)
  44. {
  45.   FILE *tfd;
  46.   LocalID lid, aiid, siid;
  47.   int i;
  48.   UInt16 cardno = 0;
  49.   UInt16 num, attr;
  50.   UInt32 uid, asz, ssz, ofst, count, total;
  51.   DmOpenRef db;
  52.   void *ap;
  53.   char buf[8];
  54.   RectangleType r;
  55.  
  56.   r.topLeft.x = 0, r.topLeft.y = 15, r.extent.x = 160, r.extent.y = 30;
  57.  
  58.   WinEraseRectangle(&r, 0);
  59.   r.topLeft.y = 30, r.extent.y = 15;
  60.  
  61.   FileTell(fd, &total, NULL);
  62.   if( destruct )
  63.     FileControl(fileOpDestructiveReadMode, fd, NULL, NULL);
  64.  
  65.   memset(&head, 0, sizeof(head));
  66.   fread(&head, sizeof(head), 1, fd);
  67.  
  68.   count = sizeof(head);
  69.  
  70.   r.extent.x = 160 * count / total;
  71.   WinDrawRectangle(&r, 0);
  72.  
  73.   WinDrawChars(head.name, strlen(head.name), 0, 15);
  74.  
  75.   if( DmCreateDatabase(cardno, head.name, head.crea, head.type,
  76.                (head.attr & dmHdrAttrResDB)) ) {
  77.     lid = DmFindDatabase(cardno, head.name);
  78.  
  79.     /////////////////CONFIRM
  80.  
  81.     if (lid)
  82.       DmDeleteDatabase(cardno, lid);
  83.     DmCreateDatabase(cardno, head.name, head.crea, head.type,
  84.              (head.attr & dmHdrAttrResDB)) ;
  85.   }
  86.  
  87.   lid = DmFindDatabase(cardno, head.name);
  88.  
  89.   if (!lid) {
  90.     fclose(fd);
  91.     return 1;
  92.   }
  93.  
  94.   head.attr &= ~dmHdrAttrReadOnly;
  95.   DmSetDatabaseInfo(cardno, lid, NULL, &head.attr, &head.vers, &head.cr,
  96.                     &head.md, &head.bkt, &head.mn, NULL, NULL, NULL, NULL);
  97.  
  98.   WinDrawChars("/", 1, 150, 0);
  99.  
  100.   {
  101.     char buf[256];
  102.  
  103.   tfd = FileOpen(0, "MAKEDBtemporary", 'DATA', 'NZIP', fileModeReadWrite, NULL);
  104.  
  105.   if (head.attr & dmHdrAttrResDB) {
  106.  
  107.     ssz = sizeof(struct rsrcdbent) * head.nrecs;
  108.     count += ssz;
  109.  
  110.     while( ssz ) {
  111.       i = ssz > 256 ? 256 : ssz;
  112.       fread(buf, 1, i , fd);
  113.       fwrite(buf, 1, i, tfd);
  114.       ssz -= i;
  115.     }
  116.  
  117.     FileControl(fileOpDestructiveReadMode, tfd, NULL, NULL);
  118.     fread(&rsrcent,1,sizeof(rsrcent),tfd);
  119.  
  120.     ofst = rsrcent.ofst;
  121.  
  122.  
  123.   } else {
  124.  
  125.  
  126.     ssz = sizeof(struct normdbent) * head.nrecs;
  127.     count += ssz;
  128.  
  129.     while( ssz ) {
  130.       i = ssz > 256 ? 256 : ssz;
  131.       fread(buf, 1, i , fd);
  132.       fwrite(buf, 1, i, tfd);
  133.       ssz -= i;
  134.     }
  135.  
  136.  
  137.     FileControl(fileOpDestructiveReadMode, tfd, NULL, NULL);
  138.     fread(&dataent,1,sizeof(dataent),tfd);
  139.  
  140.     ofst = dataent.ofst;
  141.  
  142.   }
  143.  
  144.   }
  145.  
  146.   r.extent.x = 160 * count / total;
  147.   WinDrawRectangle(&r, 0);
  148.  
  149.  
  150.   db = DmOpenDatabase(cardno, lid, dmModeReadWrite);
  151.  
  152.   asz = 0;
  153.   ssz = 0;
  154.   if (head.app)
  155.     asz = (head.sort ? head.sort : ofst) - head.app;
  156.   if (head.sort)
  157.     ssz = ofst - head.sort;
  158.  
  159. #define dmfread(bufP, objSize, numObj, stream) \
  160. FileDmRead((stream), bufP, 0, (objSize), (numObj), NULL)
  161.  
  162.   //    fread(buf, 2, 1, fd);
  163.  
  164.  
  165.   WinDrawChars("\\", 1, 155, 0);
  166.  
  167.   if (asz) {
  168.     while (count < head.app)
  169.       count += fread(buf, 1 , head.app - count > 8 ? 8 : head.app - count, fd);
  170.     ap = DmNewHandle(db, asz);
  171.     count += dmfread(MemHandleLock(ap), 1, asz, fd);
  172.     MemHandleUnlock(ap);
  173.     aiid = MemHandleToLocalID(ap);
  174.     DmSetDatabaseInfo(cardno, lid, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  175.                       &aiid, NULL, NULL, NULL);
  176.  
  177.   }
  178.  
  179.   if (ssz) {
  180.     while (count < head.sort)
  181.       count += fread(buf, 1, head.sort - count > 8 ? 8 : head.sort - count, fd);
  182.     ap = DmNewHandle(db, ssz);
  183.     count += dmfread(MemHandleLock(ap), 1, ssz, fd);
  184.     MemHandleUnlock(ap);
  185.     siid = MemHandleToLocalID(ap);
  186.     DmSetDatabaseInfo(cardno, lid, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  187.                       NULL, &siid, NULL, NULL);
  188.   }
  189.  
  190.   r.extent.x = 160 * count / total;
  191.   WinDrawRectangle(&r, 0);
  192.  
  193.   if (head.attr & dmHdrAttrResDB) {
  194.     for (i = 0; i < head.nrecs; i++) {
  195.  
  196.       if( i < head.nrecs - 1 )
  197.     fread(&rsrcnxt,1,sizeof(struct rsrcdbent),tfd);
  198.       else
  199.     rsrcnxt.ofst = total;
  200.       ssz = rsrcnxt.ofst - rsrcent.ofst;
  201.  
  202.       ap = DmNewResource(db, rsrcent.rsrc, rsrcent.rsid, ssz);
  203.       while (count < rsrcent.ofst)
  204.     count += fread(buf, 1, rsrcent.ofst - count > 8 ? 8 : rsrcent.ofst - count, fd);
  205.       count += dmfread(MemHandleLock(ap), 1, ssz, fd);
  206.       MemHandleUnlock(ap);
  207.       DmReleaseResource(ap);
  208.  
  209.       rsrcent = rsrcnxt;
  210.  
  211.       r.extent.x = 160 * count / total;
  212.       WinDrawRectangle(&r, 0);
  213.  
  214.     }
  215.  
  216.   } else {
  217.     for (i = 0; i < head.nrecs; i++) {
  218.  
  219.       if( i < head.nrecs - 1 )
  220.     fread(&datanxt,1,sizeof(struct normdbent),tfd);
  221.       else
  222.     datanxt.ofst = total;
  223.       ssz = datanxt.ofst - dataent.ofst;
  224.  
  225.       num = 0xffff;
  226.       ap = DmNewRecord(db, &num, ssz);
  227.       while (count < dataent.ofst)
  228.     count += fread(buf, 1, dataent.ofst - count > 8 ? 8 : dataent.ofst - count, fd);
  229.       count += dmfread(MemHandleLock(ap), 1, ssz, fd);
  230.       MemHandleUnlock(ap);
  231.  
  232.       attr = dataent.uid >> 24;
  233.       uid = dataent.uid & 0xffffffUL;
  234.       DmSetRecordInfo(db, num, &attr, &uid);
  235.       DmReleaseRecord(db, num, false);
  236.  
  237.       rsrcent = rsrcnxt;
  238.  
  239.       r.extent.x = 160 * count / total;
  240.       WinDrawRectangle(&r, 0);
  241.  
  242.     }
  243.  
  244.   }
  245.  
  246.   DmCloseDatabase(db);
  247.   fclose(tfd);
  248.   unlink("MAKEDBtemporary");
  249.   return 0;
  250.  
  251. }
  252.